package com.example.weblending.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.weblending.domain.ApplyNum;
import com.example.weblending.domain.ApplyUnset;
import com.example.weblending.domain.User;
import com.example.weblending.exception.GlobalException;
import com.example.weblending.mapper.UserMapper;
import com.example.weblending.service.ApplyNumService;
import com.example.weblending.mapper.ApplyNumMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
public class ApplyNumServiceImpl extends ServiceImpl<ApplyNumMapper, ApplyNum>
implements ApplyNumService{
    @Resource
    private UserMapper userMapper;

    @Override
    public boolean apply(User user,int type,int num) {
        ApplyNum applyNum=new ApplyNum();
        applyNum.setApplytime(new Timestamp(System.currentTimeMillis()));
        applyNum.setUid(user.getUid());
        applyNum.setNum(num);
        applyNum.setType(type);
        applyNum.setState(0);
        return this.save(applyNum);
    }

    @Override
    public boolean pass(ApplyNum applyNum, User operator) {
        applyNum.setState(1);
        applyNum.setOid(operator.getUid());
        applyNum.setDealtime(new Timestamp(System.currentTimeMillis()));
        boolean success= this.updateById(applyNum);
        if(!success){
            throw new GlobalException(500,"系统发生错误");
        }
        User user=userMapper.selectById(applyNum.getUid());
        if(applyNum.getType()==0){
            user.setCleannum(user.getCleannum()+applyNum.getNum());
        }
        else if(applyNum.getType()==1){
            user.setAnalyzenum(user.getAnalyzenum()+applyNum.getNum());
        }
        return userMapper.updateById(user)==1;
    }

    @Override
    public boolean reject(ApplyNum applyNum, User operator) {
        applyNum.setState(-1);
        applyNum.setOid(operator.getUid());
        applyNum.setDealtime(new Timestamp(System.currentTimeMillis()));
        boolean success= this.updateById(applyNum);
        if(!success){
            throw new GlobalException(500,"系统发生错误");
        }
        return success;
    }

    @Override
    public Page<ApplyNum> getListyId(long page, long limit, User user) {
        QueryWrapper<ApplyNum> wrapper = new QueryWrapper<>();
        wrapper.eq("state",0);
        wrapper.orderByAsc("applytime");
        Page<ApplyNum> pageParam = new Page<>(page,limit);
        baseMapper.selectPage(pageParam,wrapper);
        List<ApplyNum> list=pageParam.getRecords();
        for (int i = 0; i < list.size(); i++) {
            ApplyNum temp=list.get(i);
            User applyUser=userMapper.selectById(temp.getUid());
            temp.setName(applyUser.getName());
            temp.setEmail(applyUser.getEmail());
            temp.setUserStatue(applyUser.getStatus());
            temp.setCleannum(applyUser.getCleannum());
            temp.setAnalyzenum(applyUser.getAnalyzenum());
        }
        //map返回
        return pageParam;
    }

    @Override
    public Page<ApplyNum> getHistoryId(long page, long limit, User user) {
        QueryWrapper<ApplyNum> wrapper = new QueryWrapper<>();
        wrapper.eq("oid",user.getUid());
        wrapper.orderByDesc("dealtime");
        Page<ApplyNum> pageParam = new Page<>(page,limit);
        baseMapper.selectPage(pageParam,wrapper);
        List<ApplyNum> list=pageParam.getRecords();
        for (int i = 0; i < list.size(); i++) {
            ApplyNum temp=list.get(i);
            User applyUser=userMapper.selectById(temp.getUid());
            temp.setName(applyUser.getName());
            temp.setEmail(applyUser.getEmail());
            temp.setUserStatue(applyUser.getStatus());
        }
        //map返回
        return pageParam;
    }

    @Override
    public int getNum(User user,boolean finish) {
        QueryWrapper<ApplyNum> wrapper = new QueryWrapper<>();
        wrapper.eq("oid",user.getUid());
        if (finish){
            wrapper.ne("state",0);
        }else{
            wrapper.eq("state",0);
        }
        return baseMapper.selectCount(wrapper);
    }
}




